<?php
require_once('include/inc_database.php');

function TrimRelPath($path) {
	// Function exists in /g/index.php, /g/image/index.php, and
	// /g/src/index.php.

	// Remove the first slash, if present.
	while (substr($path, 0, 1) == "/")
		$path = substr($path, 1);
	// Remove the last slash, if present.
	while (substr($path, -1) == "/")
		$path = substr($path, 0, strlen($path)-1);
	return $path;
}

function OutputHeaders($filepath) {
	//header("Last-Modified: " . gmdate(r, time()));
	header("Last-Modified: " . gmdate(r, filemtime($filepath)));

	header("Content-type: image/jpeg");
}

function OutputImage($img_abs_path, $max_dimension) {

	$THUMB_CACHE_ROOT = $_SERVER['DOCUMENT_ROOT'] . "/../thumb_cache";

	// Remove the first slash, if present.
	//$img_web_path = TrimRelPath($img_web_path);

	//$img_abs_path = $_SERVER['DOCUMENT_ROOT'] . "/" . $img_web_path;
	//$thumb_abs_path = $THUMB_CACHE_ROOT . "/" . $img_web_path . "/m-" . $max_dimension . ".jpg";

    // Check path to prevent illegal access to other files
    if(strstr($img_abs_path, "..")) {
        echo "Illegal access!";
		exit;
    }

	// Read file from cache.
	/*if (file_exists($thumb_abs_path)) {
		OutputHeaders($thumb_abs_path);
		readfile($thumb_abs_path);
		exit();
	}*/

	$extension = substr($img_abs_path, -3);
    
	//if ($headers = apache_response_headers()) {
		//$ifmodsince = $headers['If-Modified-Since'];
		//echo "-" . $ifmodsince;
		//exit();
	//}

	if(strtolower($extension) == "jpg") {
		if (!$src_img = imagecreatefromjpeg($img_abs_path)) {
			echo "Error opening Image file.";
			exit();
		}
    }
	/*else if(strtolower($ext) == "gif") { 
		if (!$src_img = imagecreatefromgif($img_abs_path)) {
			echo "Error opening Image file!";exit;
		}
	}*/
	else {
		echo "File type not supported.";
		exit();
    }

	// Set thumb_w, thumb_h according to max_dimension.
	$source_w = imagesx($src_img);
	$source_h = imagesy($src_img);
	if ($source_w < $max_dimension+0 && $source_h < $max_dimension+0) {
		OutputHeaders($img_abs_path);
		imagejpeg($src_img);
		exit();
	}
	elseif ($source_w > 1000 || $source_h > 1000) {
		echo "Source image too big for auto scaling.  (" . $source_w . "/" . $source_h . ")";
		exit();
	}
	elseif ($source_w > $source_h) {
		$thumb_w = $max_dimension;
		$thumb_h = $max_dimension * ($source_h / $source_w);
	}
	else {
		$thumb_w = $max_dimension * ($source_w / $source_h);
		$thumb_h = $max_dimension;
	}

    $dst_img = @imagecreatetruecolor($thumb_w, $thumb_h);
	if (!$dst_img) {
		$dst_img = imageCreate($thumb_w, $thumb_h);
	}

	imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $source_w, $source_h);

	// Save thumbnail to cache if it's small enough.
	/*if ($max_dimension <= 150) {
		mkdir(dirname($thumb_abs_path) . "/", 0777, true);
		imagejpeg($dst_img, $thumb_abs_path);
	}*/

	OutputHeaders($img_abs_path);
    imagejpeg($dst_img, null, 90);
    ImageDestroy($src_img); 
    ImageDestroy($dst_img);
	return;
} 


// Get absolute path to image.
$usericon = $_GET['usericon'];
$usericon = TrimRelPath($usericon);

if ($usericon != "") {
	$abs_path = realpath($_SERVER['DOCUMENT_ROOT'] . "/" . SiteConst('storage_path')) . "/usericons/" . $usericon;
}
else {
	echo "no path supplied.";
	exit();
}

// Do meager security check.
if (strpos($abs_path, "..") !== false) {
	echo "bad path.";
	exit();
}


if ($_GET['size'] != "")
	$size = $_GET['size'];
else
	$size = "64";

if (!is_numeric($size) || $size > 2000 || $size < 16) {
	echo "size out of range.";
	exit();
}


OutputImage($abs_path,$size);
?>